package com.objectcarpentry.kaylee.ui.controls;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.menus.WorkbenchWindowControlContribution;

import com.objectcarpentry.kaylee.IClockTime;
import com.objectcarpentry.kaylee.IPlayerEvent;
import com.objectcarpentry.kaylee.IPlayerListener;
import com.objectcarpentry.kaylee.KayleePlugin;

public class ClockContribution extends WorkbenchWindowControlContribution {

	protected Label label;
	private Thread clockMan;
	
	
	public ClockContribution() {
	}

	public ClockContribution(String id) {
		super(id);
	}

	@Override
	protected Control createControl(Composite parent) {
		
		Composite c = new Composite(parent, SWT.NONE);
		c.setLayout(new FillLayout());
		label = new Label (c, SWT.NONE);
		Font initialFont = label.getFont();
		FontData[] fontData = initialFont.getFontData();
		for (int i = 0; i < fontData.length; i++) {
			fontData[i].setHeight(20);
		}
		Font newFont = new Font(PlatformUI.getWorkbench().getDisplay(), fontData);
		label.setFont(newFont);
		
		hookPlayerListener();
		updateTime(null);
		return c;
	}
	
	protected int computeWidth(Control control) {
		int columns = 5;
		GC gc = new GC (label);
		FontMetrics fm = gc.getFontMetrics ();
		int width = columns * fm.getAverageCharWidth ();
		int height = fm.getHeight ();
		gc.dispose ();
		return label.computeSize (width, height).x + 20;
    }

	private void updateTime(final IClockTime t) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				if (t == null){
					label.setText("00:00");
				} else {
					String min_s;
					String sec_s;
					int min = t.getMinutes();
					int sec = t.getSeconds();
					
					if (min < 10) {
						min_s = "0" + min;
					} else {
						min_s = String.valueOf(min);
					}
					if (sec < 10) {
						sec_s = "0" + sec;
					} else {
						sec_s = String.valueOf(sec);
					}
					
					
					label.setText(min_s + ":" + sec_s);
					label.pack();
				}
			}
		});
	}

	private void hookPlayerListener() {
		KayleePlugin.getDefault().getPlayer().addPlayerListener(new IPlayerListener() {
			
			@Override
			public void handleEvent(IPlayerEvent e) {
				
				switch (e.getType()) {
				case PLAY:  { 
					clockMan = new ClockHandler();
					clockMan.start();
					break;
				}	
				case PAUSE:
				case STOP: { 
					if (clockMan != null) {
						clockMan.interrupt();
					}
				}
				}
			}
			
		});
	}
	
	class ClockHandler extends Thread  {
		
		public ClockHandler() {
			super("ClockHandler");
		}
		
		public void run() {
			while (true) {
				KayleePlugin pg = KayleePlugin.getDefault();
				if (pg != null) {
					updateTime(pg.getPlayer().getTime());
					try {
						sleep(200);
					} catch (InterruptedException e) {
						return;
					}
				} else {
					// shutting down
					return;
				}
			}
		}
	}
	
}
